/*
 * Client.cpp
 *
 *  Created on: Nov 5, 2012
 *      Author: lucas
 */

#include "Client.h"
#include "../common/CommunicationSocket.h"
#include "../common/Mutex.h"
#include "../common/Lock.h"
#include <iostream>

Client::Client(CommunicationSocket* csck, Server& s) : sck(csck), sendThread(0), recvThread(0), gameThread(0), server(s), connected(false) {
	sendThread = new ClientSendingThread(*csck, *this);
	recvThread = new ClientReceivingThread(*csck, s, *this);

}

Client::~Client() {
	delete sendThread;
	delete recvThread;
	delete sck;
}

void Client::send(std::string& msg) {
	sendThread->send(msg);
	std::cout << "Client::send enviado: " << msg << std::endl;
}

bool Client::isConnected() {
	Lock l(m);
	return connected;
}

void Client::disconnect() {
	Lock l(m);
	if (connected) {
		//TODO Si el cliente estaba en un juego avisar al juego que se desconecto
		sendThread->parar();
		recvThread->parar();
		sck->shutDown();
		sck->cerrar();
		connected = false;
	}
}

void Client::join() {
	sendThread->join();
	recvThread->join();
}

void Client::iniciar() {
	sendThread->iniciar();
	recvThread->iniciar();
	connected = true;
}



